package com.transaction.utils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Random;

import javax.xml.bind.DatatypeConverter;

/**
 * @desc 随机字符串
 * @author wangbing
 */
public class RandomStringGenerator {

    /**
     * 获取一定长度的随机字符串 数字
     * @param length 指定字符串长度
     * @return 一定长度的字符串
     */
    public static String getNumRandomString(int length) {
        String base = "0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
    
    /** 
     * getLowercaseCharRandomString:(获取一定长度的小写字母随机字符串).
     * TODO(这里描述这个方法适用条件 – 可选).
     * @author wangbing
     * @param length
     * @return String
     */  
    public static String getLowercaseCharRandomString(int length) {
        String base = "abcdefghijklmnopqrstuvwxyz";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
    
    /** 
     * getUppercaseCharRandomString:(获取一定长度的大写字符串).
     * TODO(这里描述这个方法适用条件 – 可选).
     * @author wangbing
     * @param length
     * @return String
     */  
    public static String getUppercaseCharRandomString(int length) {
        String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
    
    /** 
     * getUppercaseCharRandomString:(获取一定长度的大小写字符串).
     * TODO(这里描述这个方法适用条件 – 可选).
     * @author wangbing
     * @param length
     * @return String
     */  
    public static String getCharRandomString(int length) {
       // String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String base = "23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
    
    /** 
     * getOutTradeNo:(根据前缀1-3位  以及总长度 生成订单号).
     * TODO(这里描述这个方法适用条件 – 可选).
     * @author wangbing
     * @param prefix
     * @param length
     * @param suffix
     * @return String
     * @throws ParseException 
     */  
    public static String getOutTradeNo(String prefix,int length){
        String base = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        Random random = new Random();
        StringBuffer sBuffer = new StringBuffer();
        int pre = 6;
//        length = length + pre;
        Date date = new Date();
        StringBuffer sb = new StringBuffer(prefix);
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
        String time = sdf.format(date);
        sb.append(time);
        int charL = length - sb.toString().length();
        for (int i = 0; i < charL; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        String tradNo = sb.toString();
        return tradNo;
    }
    
    
    public static void main(String[] args) throws Exception {
    	HashSet<String> hashSet = new HashSet<String>();
    	
    	for(int i = 0 ; i < 10 ; i++){
    		InetAddress ia = InetAddress.getLocalHost(); 
        	byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress(); 
        	String macStr = DatatypeConverter.printHexBinary(mac);
        	System.out.println(macStr);
    		String no = getOutTradeNo("ALIPRE",32);
    		System.out.println(no);
    		hashSet.add(no);
    	}
		System.out.println(hashSet.size());
		new Date().getTime();
    	System.out.println(getCharRandomString(16));
	}
}
